package com.zlx.sharelive.activity;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.ConsumptionBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 购买明细
 */
public class PurchaseDetailsAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<ConsumptionBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<ConsumptionBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_purchase_details;
    }

    @Override
    public void initViews() {
        super.initViews();

        chargeHistory();
        initRecyclerView();
    }

    private void chargeHistory() {
        ApiUtil.getUserApi().getConsumption(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<ConsumptionBean>>() {
            @Override
            public void onSuccess(ApiResponse<ConsumptionBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (null != data.getData().getInfo().get(0).getRecords()) {
                                for (int i = 0; i < data.getData().getInfo().get(0).getRecords().size(); i++) {
                                    if (data.getData().getInfo().get(0).getRecords().get(i).getDoType().equals("5")) {
                                        mList.add(data.getData().getInfo().get(0).getRecords().get(i));
                                    }
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<ConsumptionBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, ConsumptionBean.InfoDTO.RecordsDTO entity, int position) {
                holder.setText(R.id.tv_name, entity.getRemark());
                holder.setText(R.id.tv_time, entity.getCreateTime());
                holder.setText(R.id.tv_money, "￥" + entity.getMoney());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_purchase_details;
            }
        };

        rv_data.setAdapter(mAdapter);
    }

    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
